<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateArticleTbl extends Migrator
{
    const TBL_NAME = 'article';
    const RELATED_CATEGORY_NAME = 'category';
    const USER_TBL_NAME = 'user';

    public function change()
    {
        // create the table
        $table  =  $this->table(addTablePrefix(self::TBL_NAME),[
            'engine'=>'InnoDB',
            'comment' => '文章表',
            'signed' => false
        ]);

        $table
            ->addColumn('title', 'string',array('limit'  =>  64,'default'=>'','comment'=>'文章标题'))
            ->addColumn('brief', 'string',array('limit'  =>  125,'default'=>'','comment'=>'文章简介'))
            ->addColumn('image', 'string',array('limit'  =>  125,'default'=>'','comment'=>'话题封面'))
            ->addColumn('status', 'integer',array('signed' => false, 'default'=>1,'comment'=>'文章状态; 1正常,2草稿,3删除'))
            ->addColumn('best', 'integer',array('signed' => false, 'default'=>1,'comment'=>'精品; 1非精品,2精品'))
            ->addColumn('disallow', 'integer',array('signed' => false, 'default'=>0,'comment'=>'是否可评论'))
            ->addColumn('liked', 'integer',array('signed' => false, 'default'=>0,'comment'=>'喜欢数'))
            ->addColumn('visited', 'integer',array('signed' => false, 'default'=>0,'comment'=>'阅读数'))
            ->addColumn('commented', 'integer',array('signed' => false, 'default'=>0,'comment'=>'评论数'))
            ->addColumn('user_id', 'integer',array('signed' => false, 'comment'=>'创建者'))
            ->addColumn('topic_id', 'integer',array('signed' => false, 'default'=>0, 'comment'=>'所属话题'))
            ->addColumn('category_id', 'integer',array('signed' => false, 'comment'=>'所属分类'))
            ->addColumn('create_time', 'integer',array('signed' => false, 'default'=>0,'comment'=>'创建时间'))
            ->addColumn('update_time', 'integer',array('signed' => false, 'default'=>0,'comment'=>'修改时间'))
            ->addIndex(array('create_time'))
            ->addIndex('topic_id')
            ->addForeignKey('category_id', addTablePrefix(self::RELATED_CATEGORY_NAME), 'id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
            ->addForeignKey('user_id', addTablePrefix(self::USER_TBL_NAME), 'id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
            ->create();
    }
}
